<?php

class PartPlugin extends BasePlugin {
	protected $name = '上下页插件';
	protected $type = 'content';
	/* (non-PHPdoc)
	 * @see IPlugin::api()
	 */
	public function api() {
	
	}
	
	/* (non-PHPdoc)
	 * @see IPlugin::run()
	 */
	public function run() {
		$page = new PartService ();
		$rs = $page->getNearNews ( $_GET ['cid'], $_GET ['id'] );
		$this->view ()->assign ( "part", $rs );
	}
	
	/* (non-PHPdoc)
	 * @see IPlugin::v()
	 */
	public function v() {
	
	}

}

class PartService {
	
	public function getNearNews($cid, $id) {
		$rs = PartModel::instance ()->getNearNews ( $cid, $id );
		$page = array ('pre' => array (), 'next' => array () );
		
		if (empty ( $rs )) {
			return $page;
		}
		
		foreach ( $rs as $k => $v ) {
			if ($v ['id'] < $id) {
				$page ['pre'] = $v;
			} else {
				$page ['next'] = $v;
			}
		}
		return $page;
	
	}

}

class PartModel extends NewsModel {
	
	public function getNearNews($cid, $id) {
		$sql = "(SELECT id,title,html FROM $this->_news_list WHERE cid=$cid AND id<$id ORDER by id DESC LIMIT 1 ) UNION
		(SELECT id,title,html FROM  $this->_news_list WHERE cid=$cid AND id>$id ORDER by id ASC LIMIT 1)
		";
		return $this->fetchAll ( $sql );
	}
	
	/**
	 * 
	 * @return PageModel
	 */
	public static function instance() {
		return parent::_instance ( __CLASS__ );
	}
}